var Dropload = function(opt) {
    this.setting=opt;
    //判断状态
    this.isLockUp = false;
    this.isLockDown = false;
    this.isNoDate = false;
    //this.isOver = false;

    this.isOver = false; //加载完毕
    this.isLoad = true; //加载中,默认是在加载中

    //选择dom区域
    this.wrapArea = opt.scrollArea;
    this.scrollArea = opt.scrollArea.children[0];

    this.scrollArea.style.transition = "none";
    //距离判断
    cssTransform(this.scrollArea, "translateZ", 0.01);
    this.scrollTop = cssTransform(this.scrollArea, "translateY");
    this.footerH = "";
    this.footer;
    this.maxScroll = this.wrapArea.clientHeight - this.scrollArea.offsetHeight;

    //初始化
    this.init = function() {
        console.log(opt)
        if (opt.autoLoad) {
            opt.loadDownFn(this);
        }
        this.mscroll(opt.scrollArea, opt);
        var footer = document.createElement("footer");
        footer.innerHTML = opt.domDown.domRefresh;
        opt.scrollArea.children[0].appendChild(footer);
        this.footer=document.querySelector("footer");
        //底部的高度
        this.footerH = document.querySelector("footer").offsetHeight;
    }
    //调用初始化
    this.init();

};




Dropload.prototype.mscroll = function(wrap, callBack) {
    console.log(wrap)
    var child = wrap.children[0];
    var startPoint = 0;
    var startY = 0;
    var minY = wrap.clientHeight - child.offsetHeight;
    var step = 1;
    var lastY = 0;
    var lastTime = 0;
    var lastDis = 0;
    var lastTimeDis = 1;
    var isMove = true;
    var isFirst = true;
    var Tween = {
        easeOut: function(t, b, c, d) {
            return -c * ((t = t / d - 1) * t * t * t - 1) + b;
        },
        backOut: function(t, b, c, d, s) {
            if (typeof s == 'undefined') {
                s = 1.70158;
            }
            return c * ((t = t / d - 1) * t * ((s + 1) * t + s) + 1) + b;
        }
    };
    var _this = this;
    cssTransform(child, "translateZ", 0.01);
    wrap.addEventListener(
        'touchstart',
        function(e) {
            minY = wrap.clientHeight - child.offsetHeight;
            clearInterval(child.scroll);
            // if (callBack && callBack.start) {
            //     callBack.start();
            // }

            console.log("_this.scrollTop", _this.scrollTop)
            _this.maxScroll = _this.wrapArea.clientHeight - _this.scrollArea.offsetHeight;
            if (!_this.isOver && _this.scrollTop <= _this.maxScroll) {
                _this.isLoad = true;
                //_this.isOver = false
                console.log("没有进来？===============")
            };
            startPoint = { pageY: e.changedTouches[0].pageY, pageX: e.changedTouches[0].pageX };
            startY = cssTransform(child, "translateY");
            step = 1;
            lastY = startPoint.pageY;
            lastTime = new Date().getTime();
            lastDis = 0;
            lastTimeDis = 1;
            isMove = true;
            isFirst = true;
        }
    );
    wrap.addEventListener(
        'touchmove',
        function(e) {
            if (!isMove) {
                return;
            }
            var nowPoint = e.changedTouches[0];
            var disX = nowPoint.pageX - startPoint.pageX;
            var disY = nowPoint.pageY - startPoint.pageY;
            // if (isFirst) {
            //     isFirst = false;
            //     if (Math.abs(disY) < Math.abs(disX)) {
            //         isMove = false;
            //         return;
            //     }
            // }
            var t = startY + disY;
            var nowTime = new Date().getTime();
            //从上先向下啦
            if (t > 0) {
                step = 1 - t / wrap.clientHeight;
                t = parseInt(t * step);
            }
            //从下向上啦
            if (t < minY) {
                var over = minY - t;
                step = 1 - over / wrap.clientHeight;
                over = parseInt(over * step);
                t = minY - over;
            }
            lastDis = nowPoint.pageY - lastY;
            lastTimeDis = nowTime - lastTime;
            lastY = nowPoint.pageY;
            lastTime = nowTime;
            cssTransform(child, "translateY", t);
            if (callBack && callBack.in) {
                callBack.in();
            }
        }
    );
    wrap.addEventListener(
        'touchend',
        function() {
            var speed = (lastDis / lastTimeDis) * 120;
            speed = isNaN(speed) ? 0 : speed;
            var t = cssTransform(child, "translateY");
            var target = t + speed;
            var type = "easeOut";
            var time = Math.abs(speed * .9);
            time = time < 300 ? 300 : time;
            if (target > 0) {
                target = 0;
                type = "backOut";
            }
            if (target < minY) {
                target = minY;
                type = "backOut";
            }
            _this.move(target, time, type);
            // if (callBack && callBack.end) {
            //     callBack.end();
            // }
			 _this.scrollTop = cssTransform(_this.scrollArea, "translateY");
            if (callBack.loadDownFn) {
               
                
                if (_this.validLoadDown()) {
                    console.log("该加载数据了")
                    callBack.loadDownFn.call(_this, _this);
                } else {
                    _this.resetload();
                }
                this.isOver = true;
                this.isLoad = false;
            }

        }
    );
    /*
    	start 手指按下
    	in 滑动中
    	end 手指抬起
    	over 滑动结束
    */
    Dropload.prototype.move=function(target, time, type) {
    	var _this=this;

        var t = 0;
        var b = cssTransform(_this.scrollArea, "translateY");
        var c = target - b;
        var d = Math.ceil(time / 20);

        clearInterval(_this.scrollArea.scroll);
        
        _this.scrollArea.scroll = setInterval(
            function() {
                t++;
                if (t > d) {
                    clearInterval(_this.scrollArea.scroll);
                    if (callBack && callBack.over) {
                        callBack.over();
                    };

                } else {
                    var top = Tween[type](t, b, c, d);
                    cssTransform(_this.scrollArea, "translateY", top);
                    // if (callBack && callBack.in) {
                    //     callBack.in();
                    // }
                   

                }
            }, 20
        );
    }
}
Dropload.prototype.lock = function(direction) {
    if (direction = "down" || !direction) {
        this.isLockDown = true;
    } else {
        this.isLockUp = false;
    }
};
Dropload.prototype.resetload = function(direction) {
    console.log("刷新")
    this.scrollTop = cssTransform(this.scrollArea, "translateY");
    console.log("this.scrollTop::::", this.scrollTop)

};
Dropload.prototype.noData = function(Bool) {
    this.isNoDate = Bool ? true : false;
};
Dropload.prototype.validLoadDown = function(Bool) {

    // this.isOver = true;
    // this.isLoad = false;
    console.log("计算：", this.maxScroll, this.scrollTop, this.maxScroll - this.scrollTop)
    console.log("内容高度::", this.wrapArea.clientHeight, this.scrollArea.offsetHeight, this.wrapArea.clientHeight - this.scrollArea.offsetHeight)
    console.log("!this.isOver", !this.isOver, "this.isLoad", this.isLoad)
    if(this.isNoDate){
        this.footer.innerHTML=this.setting.domDown.domNoData;
        return false;
    };

    //剔除掉底部的高度
    if (!this.isOver && this.isLoad && this.maxScroll - this.scrollTop >= this.footerH) {
        return true;
    }
    return false;
};